package cn.ruanyun.backInterface.modules.myWeddingInvitation.serviceimpl;

import cn.ruanyun.backInterface.common.utils.PageUtil;
import cn.ruanyun.backInterface.common.utils.ResultUtil;
import cn.ruanyun.backInterface.common.vo.PageVo;
import cn.ruanyun.backInterface.common.vo.Result;
import cn.ruanyun.backInterface.modules.base.pojo.DataVo;
import cn.ruanyun.backInterface.modules.myWeddingInvitation.mapper.MyWeddingInvitationMapper;
import cn.ruanyun.backInterface.modules.myWeddingInvitation.pojo.MyWeddingInvitation;
import cn.ruanyun.backInterface.modules.myWeddingInvitation.service.IMyWeddingInvitationService;
import cn.ruanyun.backInterface.modules.myWeddingInvitation.vo.MyWeddingInvitationVo;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import cn.ruanyun.backInterface.common.utils.ToolUtil;
import cn.ruanyun.backInterface.common.utils.SecurityUtil;
import org.springframework.web.context.request.NativeWebRequest;


/**
 * 我的婚礼模板接口实现
 * @author z
 */
@Slf4j
@Service
@Transactional
public class IMyWeddingInvitationServiceImpl extends ServiceImpl<MyWeddingInvitationMapper, MyWeddingInvitation> implements IMyWeddingInvitationService {


    @Autowired
    private SecurityUtil securityUtil;

    @Override
    public Result<Object> insertOrderUpdateMyWeddingInvitation(MyWeddingInvitation myWeddingInvitation) {

        myWeddingInvitation.setCreateBy(securityUtil.getCurrUser().getId());


        if (ToolUtil.isEmpty(myWeddingInvitation.getId())) {

            if (this.save(myWeddingInvitation)) {

                myWeddingInvitation.setWeddingUrl("http://121.196.28.116:8088?id=" + myWeddingInvitation.getId());

                this.updateById(myWeddingInvitation);

                return new ResultUtil<>().setData(myWeddingInvitation.getWeddingUrl(), "获取模板文件成功！");
            }

            return new ResultUtil<>().setErrorMsg(201, "失败！");

        }else {

            return Optional.ofNullable(this.getById(myWeddingInvitation.getId())).map(myWeddingInvitationNew -> {

                ToolUtil.copyProperties(myWeddingInvitation, myWeddingInvitationNew);
                this.updateById(myWeddingInvitationNew);
                return new ResultUtil<>().setData(myWeddingInvitation.getWeddingUrl(), "获取更新模板文件成功！");
            }).orElse(new ResultUtil<>().setErrorMsg(203, "不存在此模板！"));
        }
    }

    @Override
    public Result<Object> removeMyWeddingInvitation(String ids) {

        this.removeByIds(ToolUtil.splitterStr(ids));

        return new ResultUtil<>().setSuccessMsg("移除成功！");
    }

    /**
     * 获取我的结婚模板详情
     *
     * @param id id
     * @return MyWeddingInvitationVo
     */
    @Override
    public Result<MyWeddingInvitation> getMyWeddingInvitation(String id) {

        return Optional.ofNullable(this.getById(id)).map(myWeddingInvitation -> new ResultUtil<MyWeddingInvitation>().setData(myWeddingInvitation, "获取我的模板详情成功！"))
                .orElse(new ResultUtil<MyWeddingInvitation>().setErrorMsg(201, "暂无数据！"));
    }

    /**
     * 获取我的婚礼模板列表
     *
     * @param pageVo pagevo
     * @return MyWeddingInvitationVo
     */
    @Override
    public Result<DataVo<MyWeddingInvitationVo>> getMyWeddingInvitationList(PageVo pageVo) {

        Page<MyWeddingInvitation> myWeddingInvitationPage = this.page(PageUtil.initMpPage(pageVo), Wrappers.<MyWeddingInvitation>lambdaQuery()
        .eq(MyWeddingInvitation::getCreateBy, securityUtil.getCurrUser().getId())
        .orderByDesc(MyWeddingInvitation::getCreateTime));

        if (ToolUtil.isEmpty(myWeddingInvitationPage.getRecords())) {

            return new ResultUtil<DataVo<MyWeddingInvitationVo>>().setErrorMsg(201, "暂无数据！");
        }

        DataVo<MyWeddingInvitationVo> result = new DataVo<>();
        result.setTotalPage(myWeddingInvitationPage.getPages())
                .setCurrentPageNum(myWeddingInvitationPage.getCurrent())
                .setTotalSize(myWeddingInvitationPage.getSize())
                .setDataResult(myWeddingInvitationPage.getRecords().parallelStream().flatMap(myWeddingInvitation -> {

                    MyWeddingInvitationVo myWeddingInvitationVo = new MyWeddingInvitationVo();
                    ToolUtil.copyProperties(myWeddingInvitation, myWeddingInvitationVo);

                    return Stream.of(myWeddingInvitationVo);

                }).collect(Collectors.toList()));

        return new ResultUtil<DataVo<MyWeddingInvitationVo>>().setData(result, "获取我的婚礼模板成功！");
    }
}